﻿using Commons.ASPNETCore;
using FileService.Domain;
using FileService.Domain.Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Mysqlx.Session;
using Org.BouncyCastle.Asn1.Ocsp;

namespace FileService.WebApi.Controllers
{
    [Route("BaseVideo/[controller]")]
    [ApiController]
    public class ShowTabController : ControllerBase
    {
        private readonly ITabRepository repository;
        private readonly IThumbnailService thumbnailService;
        public ShowTabController(ITabRepository repository, IThumbnailService thumbnailService)
        {
            this.repository = repository;
            this.thumbnailService = thumbnailService;
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="tabId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result> FindListByTabId(long tabId)
        {
            var list = await repository.QueryListByParentIdAsync<ShowTab>(tabId);
            return Result.Success(list);
        }

        /// <summary>
        /// 根据id获取详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<Result> Get(long id)
        {
            var o = await repository.FindByIdAsync<ShowTab>(id);
            return Result.Success(o);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stu"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public async Task<Result> Add(ShowTabRequest request)
        {
            var result = await repository.AddAsync(ShowTab.Create(request.Name, request.TabId, request.Type));
            if (result == 0)
            {
                return Result.Fail();
            }
            return Result.Success();
        }

        [Authorize]
        [HttpDelete]
        public async Task<Result> Delete([FromBody] ShowTab request)
        {
            var result = await repository.DeleteAsync(request);
            if (result == 0)
            {
                return Result.Fail();
            }
            return Result.Success();
        }
        [Authorize]
        [HttpPut]
        public async Task<Result> Put([FromBody] ShowTab request)
        {
            var o = await repository.FindByIdAsync<ShowTab>(request.Id);
            if (o == null) {
                return Result.Fail();
            }

            var re = await repository.UpdateAsync(o.Change(request.Name,request.Type));
            return Result.Success(re);
        }
       
        [Authorize]
        [HttpPut("ResetThumbnails/{id}")]
        public async Task<Result> ResetThumbnails(long id)
        {
            var o = await repository.FindByIdAsync<ShowTab>(id);
            if (o == null)
            {
                return Result.Fail();
            }

            var videoList= await  repository.QueryListByParentIdAsync<Videos>(id);

             IEnumerable<Videos> videos=  videoList.Select(x=>x.SetFile(x.FileUrl,x.FileName, thumbnailService.GenerateThumbnail(x.FileUrl))).ToList();

             var re = await repository.UpdateAsync(videos);
            return Result.Success();
        }
    }
}
